package cn.smbms.controller;

import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.BillService;
import cn.smbms.service.ProviderService;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/bill")
public class BillController {


    @Resource
    private ProviderService providerService;

    @Resource
    private BillService billService;


    @RequestMapping("/getBillList.html")
    public String getByProduct(@RequestParam(value = "queryProductName",required = false) String productName,
                               @RequestParam(value = "queryProviderId",required = false) Integer providerId,
                               @RequestParam(value = "queryIsPayment",required = false) Integer isPayment,
                               @RequestParam (value = "pageIndex", required = false,defaultValue = "1") Integer PageNo,
                               @RequestParam(value = "pageSize", required = false,defaultValue = "5") Integer pageSize,
                               Model model){

        //总数量（表）
        Integer totalCount = billService.getBillCount(productName,providerId,isPayment);

        int totalPageCount = totalCount/pageSize;
        if(totalCount%pageSize!=0){
            totalPageCount++;
        }

        //控制首页和尾页
        if(PageNo < 1){
            PageNo = 1;
        }else if(PageNo > totalPageCount){
            PageNo = totalPageCount;
        }





        List<Provider> providerList = providerService.getProList();
        List<Bill> billList = billService.getByProduct(productName, providerId, isPayment,PageNo,pageSize);
        model.addAttribute("billList",billList);
        model.addAttribute("providerList",providerList);


        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", PageNo);



        return "billlist";
    }



    @RequestMapping(value = "/view/{id}")
    public String viewBill(Model model,
                              @PathVariable(value = "id") Integer id){
        Bill bill = billService.getBillById(id);
        model.addAttribute("bill",bill);
        return "billview";

    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam(value = "billid") Integer id){
        boolean check = billService.delete(id);
        HashMap<String,String> data = new HashMap<String,String>();
        if(id!=null){
            if(check){
                data.put("delResult","true");
            }else{
                data.put("delResult","false");
            }
        }else{
            data.put("delResult","notexist");
        }

        return JSONArray.toJSONString(data);
    }


    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    public String modifyUser(Bill bill,Model model){
        boolean check = billService.modify(bill);
        if(!check){
            return "billmodify";
        }
        return "redirect:/bill/getBillList.html";
    }


    @RequestMapping(value = "/toModify/{id}")
    public String toModify(Model model,
                           @PathVariable(value = "id") Integer id){
        Bill bill = billService.getBillById(id);
        List<Provider> providerList = providerService.getProList();
        model.addAttribute("providerList",providerList);
        model.addAttribute("bill",bill);

        return "billmodify";
    }


    @RequestMapping(value = "toAdd")
    public String toAdd(Bill bill,
                        Model model){
        List<Provider> providerList = providerService.getProList();
        model.addAttribute("providerList",providerList);

        return "billadd";
    }

    @RequestMapping(value = "/add.html")
    public String add(Bill bill){
        bill.setCreationDate(new Date());
        boolean check = billService.add(bill);
        if(check){
            return "redirect:/bill/getBillList.html";
        }
        return "billadd";
    }


}
